﻿using Common.Prism.Apps;
using NxChatQQ.Models;
using NxChatQQ.Models.Events;
using NxChatQQ.Views;
using NxChatQQ.Views.Pages;
using Prism.Commands;
using Prism.Regions;
using System.Windows.Input;

namespace NxChatQQ.ViewModels
{
    public class MainWindowViewModel : BaseViewModel
    {

        private string _InputText = "Hello_Test";
        public string InputText
        {
            get { return _InputText; }
            set { SetProperty(ref _InputText, value); }
        }


        private int _NavIndex = 0;
        public int NavIndex
        {
            get { return _NavIndex; }
            set { SetProperty(ref _NavIndex, value); }
        }


        private int _FootNavIndex = -1;
        public int FootNavIndex
        {
            get { return _FootNavIndex; }
            set { SetProperty(ref _FootNavIndex, value); }
        }



        private bool _IsWhiteBackground;
        public bool IsWhiteBackground
        {
            get { return _IsWhiteBackground; }
            set { SetProperty(ref _IsWhiteBackground, value); }
        }



        public ICommand NavChangedCommand { get; set; }
        public ICommand FootNavChangedCommand { get; set; }

        public readonly IRegionManager _regionManager;


        public MainWindowViewModel(IRegionManager regionManager)
        {

            NavChangedCommand = new DelegateCommand<int?>(NavSelectionChanged);
            FootNavChangedCommand = new DelegateCommand<int?>(FootNavSelectionChanged);

            // 默认加载聊天页
            _regionManager = regionManager;
            regionManager.RegisterViewWithRegion("MainViewContent", typeof(MainPage));

            // 订阅事件
            Global.EventAggregator.GetEvent<UpdateFriendInfoEvent>().Subscribe(UpdateChatMethod);
            Global.EventAggregator.GetEvent<UpdateNavEvent>().Subscribe(UpdateNavMethod);
        }

        private void UpdateNavMethod(int obj)
        {
            NavIndex = obj;
        }

        private void UpdateChatMethod(Friend friend)
        {
            if (friend == null)
            {
                IsWhiteBackground = false;
            }
            else
            {
                IsWhiteBackground = true;
            }
        }

        private void FootNavSelectionChanged(int? index)
        {
            if (FootNavIndex == 0)
            {
                FootNavIndex = -1;

                // 显示设置窗口
                Global.WindowService.Show<SettingWindow>();
            }
        }

        private void ChangePageView(string left, string right)
        {
            _regionManager.RequestNavigate("ChatListRegion", left);
            _regionManager.RequestNavigate("ChatPageRegion", right);
        }

        private void NavSelectionChanged(int? index)
        {
            switch (NavIndex)
            {
                case 0:
                    {
                        // 显示聊天页
                        IsWhiteBackground = false;
                        ChangePageView("RecentListLayout", "ChatMessageLayout");
                    }
                    break;
                case 1:
                    {
                        // 显示好友页
                        ChangePageView("FriendListLayout", "FriendInfoLayout");
                    }
                    break;
                case 2:
                    {
                        // 显示收藏页
                        IsWhiteBackground = false;
                        ChangePageView("CollectListLayout", "CollectContentLayout");
                    }
                    break;

                default:
                    break;
            }
        }


        //public void CloseDialogCompleted()
        //{
        //    IsDialogQuit = false;
        //    IsDialogOpen = false;
        //    AllowClose = true;
        //    _regionManager.Regions["DialogHostContent"]?.RemoveAll();
        //}

        //public void OnNotifyMainModelView(DialogContent content)
        //{
        //    if (content != null)
        //    {
        //        IsDialogQuit = false;
        //        IsDialogOpen = true;
        //        AllowClose = content.AllowClose;
        //        _regionManager.RequestNavigate("DialogHostContent", content.ViewName, content.Parameters);
        //    }
        //    else
        //    {
        //        IsDialogQuit = true;
        //        //AllowClose = true;
        //        //_regionManager.Regions["DialogHostContent"]?.RemoveAll();
        //    }
        //}
    }
}
